/*
 * Machine.java
 *
 * Stateroom - Java framework library for finite state machines
 * Copyright (C) 2009 by Matthew Werny
 * All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.iceflow.stateroom;

import net.iceflow.stateroom.exception.DefinitionFailedException;
import net.iceflow.stateroom.exception.InvalidContextException;
import net.iceflow.stateroom.interfaces.Context;
import net.iceflow.stateroom.interfaces.MachineStrategy;
import net.iceflow.stateroom.interfaces.Model;
import net.iceflow.stateroom.interfaces.StateDiscriminator;

/**
 * Finite state machine container to allow the running of machine strategies.
 * @param <M> Type used to represent the machine model
 */
public interface Machine<M extends Model> {
	
	/**
	 * Evaluates a context until it either crashes or enters an accept state.
	 * <p>
	 * WARNING: If your machine strategy defines a machine that is Turing complete, or is otherwise capable of looping forever,
	 * this method is also capable of looping forever and not returning. Approach usage of this method with caution.
	 * </p>
	 * @param context Context to evaluate
	 * @throws InvalidContextException If the provided context model is determined to be invalid
	 */
	void evaluateUntilHalted(final Context<M> context) throws InvalidContextException;
	
	/**
	 * Evaluates a context for one evaluation cycle, performs the appropriate state update and returns.
	 * @param context Context to evaluate
	 * @throws InvalidContextException If the provided context model is determined to be invalid
	 */
	void evaluate(final Context<M> context) throws InvalidContextException;
	
	/**
	 * Creates a newly initialized context representing a specific machine instance.
	 * @return Newly create context
	 */
	Context<M> newContext();
	
	/**
	 * Factory for constructing new implementation instances of the Machine interface.
	 */
	public static final class Factory {

		/**
		 * Intentionally does nothing else but prevent instantiation of this factory class.
		 */
		protected Factory() {
                    throw new UnsupportedOperationException("Instantiation not allowed.");
		}

		/**
		 * Builds a new implementation of the Machine interface using the definition in the provided strategy and uses the provided discriminator.
		 * @param <T> Model type used by the machine
		 * @param strategy Machine definition strategy
		 * @param discriminator State discriminator to be used by the machine
		 * @return Newly built machine
		 * @throws DefinitionFailedException If the machine fails to be defined without error
		 */
		public static <T extends Model> Machine<T> build(final MachineStrategy<T> strategy, final StateDiscriminator<T> discriminator) throws DefinitionFailedException {
			final DefaultMachineImpl<T> machine = new DefaultMachineImpl<T>(strategy, discriminator);
			machine.attemptMachineDefinition();
			
			return machine;
		}

		/**
		 * Builds a new implementation of the Machine interface using the definition in the provided strategy and uses a default state discriminator.
		 * @param <T> Model type used by the machine
		 * @param strategy Machine definition strategy
		 * @return Newly built machine
		 * @throws DefinitionFailedException If the machine fails to be defined without error
		 */
		public static <T extends Model> Machine<T> build(final MachineStrategy<T> strategy) throws DefinitionFailedException {
			final DefaultMachineImpl<T> machine = new DefaultMachineImpl<T>(strategy);
			machine.attemptMachineDefinition();
			
			return machine;
		}
	}
}
